package com.contract.spirit.func.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.contract.spirit.R;
import com.google.android.material.tabs.TabLayout;


public class CustomRankTabLayout extends TabLayout {
    private Context mContext;
    private ViewPager viewpager;
    private int normalTextColor;
    private int selectTextColor;
    private int tabLineColor;
    private float normalTextSize;
    private float selectTextSize;
    private boolean isBold;
    private int tabLineHeight;
    private int tabLineWidth;

    public CustomRankTabLayout(Context context) {
        super(context);
        initView(context, null);
    }

    public CustomRankTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        if (attrs != null) {
            @SuppressLint("CustomViewStyleable") TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTab);
            //默认颜色
            normalTextColor = array.getColor(R.styleable.CustomTab_customNormalTextColor, getResources().getColor(R.color.colorTextGrey));//颜色
            //选中时颜色
            selectTextColor = array.getColor(R.styleable.CustomTab_customSelectTextColor, getResources().getColor(R.color.colorTextGrey));//颜色
            //下划线颜色
            tabLineColor = array.getColor(R.styleable.CustomTab_customTabLineColor, getResources().getColor(R.color.colorTextGrey));//颜色
            //默认字体大小
            normalTextSize = array.getDimensionPixelSize(R.styleable.CustomTab_customNormalTextSize, 14);
            //选中时字体大小
            selectTextSize = array.getDimensionPixelSize(R.styleable.CustomTab_customSelectTextSize, 14);
            //选中时字体是否加粗
            isBold = array.getBoolean(R.styleable.CustomTab_customSelectIsBold, true);
            //下划线高度
            tabLineHeight = array.getDimensionPixelSize(R.styleable.CustomTab_customTabLineHeight, 3);
            //下划线宽度
            tabLineWidth = array.getDimensionPixelSize(R.styleable.CustomTab_customTabLineWidth, 30);
            this.setTabIndicatorFullWidth(false);
            this.setSelectedTabIndicatorColor(mContext.getResources().getColor(R.color.colorPrimary));
            this.setTabMode(MODE_FIXED);
            array.recycle();
        }
    }

    //initTab方法之前调用
    public void initViewPager(ViewPager viewPager) {
        this.viewpager = viewPager;
        this.setupWithViewPager(viewPager);
    }

    /**
     * @desc 初始化tab栏
     * @author zhengjun
     * @created at 2018/12/21 9:58
     */
    public void initTab(String titles[]) {
        for (int i = 0; i < titles.length; i++) {
            Tab tab = getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.layout_custom_rank_tab);
                View customView = tab.getCustomView();
                if (customView != null) {
                    LinearLayout layout = tab.view;
                    layout.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
                    TextView tv_tab_title = customView.findViewById(R.id.tv_tab_title);
                    tv_tab_title.setText(titles[i]);
                    if (i == 0) {
                        setSelectStyle(tab);
                    }
                }
            }
        }
        addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                if (tab.getCustomView() != null) {
                    setSelectStyle(tab);
                }
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(Tab tab) {
                if (tab.getCustomView() != null) {
                    setUnSelectStyle(tab);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {

            }
        });
    }

    //默认的样式
    private void setUnSelectStyle(Tab tab) {
        View customView = tab.getCustomView();
        if (customView != null) {
            TextView tv_tab_title = customView.findViewById(R.id.tv_tab_title);
            View view_divider = customView.findViewById(R.id.view_divider);
            ImageView ivIcon = customView.findViewById(R.id.iv_tabIcon);
            if (tab.getPosition()==0){
                ivIcon.setVisibility(VISIBLE);
            }else {
                ivIcon.setVisibility(GONE);
            }
            tv_tab_title.setTextColor(normalTextColor);
            tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, normalTextSize);
            tv_tab_title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            view_divider.setVisibility(View.INVISIBLE);
        }
    }
    //选中时的样式
    private void setSelectStyle(Tab tab) {
        View customView = tab.getCustomView();
        if (customView != null) {
            TextView tv_tab_title = customView.findViewById(R.id.tv_tab_title);
            View view_divider = customView.findViewById(R.id.view_divider);
            ImageView ivIcon = customView.findViewById(R.id.iv_tabIcon);
            if (tab.getPosition()==0){
                ivIcon.setVisibility(VISIBLE);
            }else {
                ivIcon.setVisibility(GONE);
            }
            tv_tab_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, selectTextSize);
            tv_tab_title.setTextColor(selectTextColor);
            if (isBold) {
                tv_tab_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
            ViewGroup.LayoutParams layoutParams = view_divider.getLayoutParams();
            layoutParams.width = tabLineWidth;
            layoutParams.height = tabLineHeight;
            view_divider.setVisibility(View.VISIBLE);
            view_divider.setBackgroundColor(tabLineColor);
        }
    }
}
